package xdj.android.personal.namespace.Views;

import java.util.ArrayList;
import java.util.List;

import xdj.android.personal.R;
import xdj.android.personal.namespace.Business.BaseDBAccess;
import xdj.android.personal.namespace.Business.LineDBAccess;
import xdj.android.personal.namespace.Entity.IDNameEntity;
import xdj.android.personal.namespace.Entity.LineEntity;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MyListActivity<Entity extends IDNameEntity,Business extends BaseDBAccess<Entity,Entity>> extends ListActivity
{

	Business _dBAccess;
	private final static String TAG = "MyListActivity";

	private Handler handler;
	private MyListAdapter adapter;
	private Runnable geRunnableHandler = new Runnable()
	{
		public void run()
		{
			setAdapter();
		}
	};
     
	public MyListAdapter GetMyAdapter()
	{
		return adapter;
	}
	
	public void SetBusiness(Business bus)
	{
		_dBAccess = bus;
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.mylistview);
		//lineDBAccess = new LineDBAccess(this);

		handler = new Handler();

		handler.postDelayed(geRunnableHandler, 100);

	}

	private void setAdapter()
	{
		List<Entity> list = getLineList();

		adapter = new MyListAdapter(list);
		ListView lView = this.getListView();
		lView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		lView.setAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{

		super.onListItemClick(l, v, position, id);
		Log.v(TAG + "getItem", adapter.getItem(position).toString());
		Log.v(TAG + "getItemId", String.valueOf(adapter.getItemId(position)));
		Log.v(TAG + "getItemViewType",
				String.valueOf(adapter.getItemViewType(position)));

	}

	@Override
	protected void onDestroy()
	{
		// TODO Auto-generated method stub
		super.onDestroy();
		handler.removeCallbacks(geRunnableHandler);
	}

	private List<Entity> getLineList()
	{
		return _dBAccess.GetAllQueryResults();
	}

	
	public class MyListAdapter<Entity extends IDNameEntity> extends BaseAdapter
	{

		// private Context context;
		private ArrayList<Entity> lines;

		public MyListAdapter(List<Entity> list)
		{
			// context = c;
			lines = (ArrayList<Entity>) list;
		}

		public int getCount()
		{

			return lines.size();
		}

		public Object getItem(int position)
		{

			return lines.get(position);
		}

		public long getItemId(int position)
		{

			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			if (convertView == null)
			{
				convertView = getLayoutInflater().inflate(
						R.layout.line_item_view, null);
			}
			TextView txtLineId = (TextView) convertView
					.findViewById(R.id.lineItemViewTxtLineId);
			String lineidString = lines.get(position).ID;
			txtLineId.setText(lineidString);

			TextView txtLineName = (TextView) convertView
					.findViewById(R.id.lineItemViewTxtLineName);
			String linenameString = lines.get(position).Name;
			txtLineName.setText(linenameString);
			return convertView;
		}
	}
}
